import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class test2 {
    //18. 四数之和 双指针算法题目
    public  static List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        int nleft=0;
        while(nleft<nums.length-3){
            int temp=target-nums[nleft];
            int ret = nums.length - 1;//放里面进行刷新
            while ( ret > 2) {
                int right = ret - 1;
                int left = nleft+1;
                while (left < right) {
                    if (nums[left] + nums[ret] + nums[right] > temp) {
                        right--;
                    }
                    else if (nums[left] + nums[ret] + nums[right] < temp) {
                        left++;
                    }
                    else {
                        list.add(new ArrayList<>(Arrays.asList(nums[nleft],nums[left],nums[right],nums[ret])));
                        left++;
                        right--;
                        while (left<right&&nums[left-1]==nums[left])left++;
                        while (left<right&&nums[right+1]==nums[right])right--;
                    }

                }
                ret--;
                while (ret >2&&nums[ret+1]==nums[ret])ret--;
            }
            nleft++;
            while(nleft<nums.length-3&&nums[nleft-1]==nums[nleft])nleft++;
        }
        return list;
    }

    public static void main(String[] args) {
        int []arr={1000000000,1000000000,1000000000,1000000000};
        List<List<Integer>> list = new ArrayList<>();
        list=fourSum(arr,-294967296);
        System.out.println(list.toString());

    }
}
